﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using WAS.Logic;
using WAS.Menu;
using Utilities.Menu.UIElements;
using WAS.Resources;
using WAS.Logic.SampleLevel;

namespace WAS
{

    using Game = Microsoft.Xna.Framework.Game;
    using Utilities;

    /// <summary>
    /// La classe UIRenderer si occupa di disegnare l'interfaccia: punteggi, informazioni utili, bottoni (il pulsante "QUIT GAME") etc.
    /// UIRenderer utilizza il servizio di IStateProvider per conoscere punteggio, numero di vite residue del giocatore, etc.
    /// </summary>
    public class UILandscape : DrawableGameComponent
    {
        public static int PrimaryActive = 0, SecondaryActive = 0;

        Matrix screen_transform;
        SpriteBatch sprite_batch;
        SpriteFont title_font, arial14, menu_f;
        Texture2D white_pixel, ship_icon, life_bar;
        Button resume, quit_to_menu, controls, audio, quit_and_save;
        SoundEffect click;
        Texture2D tutorial1, tutorial2, tutorial3, tutorial4, tutorial5, tutorial6, tutorial7, tutorial8, tutorial9;
        int tutorial_page = 0;
        Rectangle top_left, middle_left, mini_top_left, mini_middle_left, mini_bottom_left, mini_top_middle, mini_bottom_middle;

        float alpha = 0.0f;
        Texture2D single_tap, double_tap, long_tap;
        SpriteFont font_32, font_16, font12;
        Button back;
        Button[] weapons, wtype;
        Box box;
        Texture2D ui_dock, overlay_dock, bar_block, score_dock, top_menu_border;
        Rectangle pause_rect, black_rect, top_menu_border_rect;
        Vector2 box_dimension = new Vector2(200, 200);
        Vector2 initial_box_pos = new Vector2(1100, 252.5f), final_box_pos = new Vector2(590, 252.5f);
        GraphicsDeviceManager graphics;
        Button[] tutorial_buttons;

        xAnimations anim1, anim2, anim3;

        PlayState state;

        public UILandscape(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            this.UpdateOrder = (int)ComponentOrders.UI;
            this.DrawOrder = (int)ComponentOrders.UI;

        }

        IStateProvider<BaseLevelStats> state_provider;
        CameraProvider camera;
        InputManager input_manager;
        public static ShipType last_ship_selected = ShipType.PlayerBaseShip;
        public override void Initialize()
        {
            state_provider = Game.Services.GetService(typeof(IStateProvider<BaseLevelStats>))
              as IStateProvider<BaseLevelStats>;
            (Game.Services.GetService(typeof(IInputManager)) as IInputManager).OnTap += new InputManager.Tap(on_tap);
            graphics = (GraphicsDeviceManager)Game.Services.GetService(typeof(GraphicsDeviceManager));

            input_manager = Game.Services.GetService(typeof(IInputManager)) as InputManager;

            camera = Game.Services.GetService(typeof(CameraProvider)) as CameraProvider;

            state = PlayState.Playing;

            base.Initialize();
        }

        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(IInputManager));

            //graphics.SupportedOrientations = DisplayOrientation.Portrait;
            //graphics.PreferredBackBufferWidth = 480;
            //graphics.PreferredBackBufferHeight = 800;

            //graphics.ApplyChanges();

            base.Dispose(disposing);
        }

        protected override void LoadContent()
        {
            audio_manager = Game.Services.GetService(typeof(IAudioManager)) as AudioManager;
            sprite_batch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            screen_transform = Matrix.CreateScale(GraphicsDevice.Viewport.Width / 800,
                GraphicsDevice.Viewport.Height / 480, 1.0f);

            title_font = Game.Content.Load<SpriteFont>(@"fonts\OCRAExtended28");
            arial14 = Game.Content.Load<SpriteFont>(@"fonts\arial14ptx");
            menu_f = Game.Content.Load<SpriteFont>(@"fonts\OCRAExtended18");
            white_pixel = Game.Content.Load<Texture2D>(@"UI\whitepixel");
            ship_icon = Game.Content.Load<Texture2D>(@"UI\ship_ico_v");
            click = Game.Content.Load<SoundEffect>(@"Audio\button");
            ui_dock = Game.Content.Load<Texture2D>(@"UI\upper_dock_v");
            score_dock = Game.Content.Load<Texture2D>(@"UI\score_dock");
            menu_dock = Game.Content.Load<Texture2D>(@"UI\menu_dock");
            black_box = Game.Content.Load<Texture2D>(@"UI\black_box");
            bar_block = Game.Content.Load<Texture2D>(@"UI\bar_block_v");
            life_bar = Game.Content.Load<Texture2D>(@"UI\life_bar");
            yellow_glow = Game.Content.Load<Texture2D>(@"UI\WSelector\yellow_glow");
            white_glow = Game.Content.Load<Texture2D>(@"UI\WSelector\white_glow");
            f_weapon_circle = Game.Content.Load<Texture2D>(@"UI\WSelector\weapon_circle_v");
            s_weapon_circle = Game.Content.Load<Texture2D>(@"UI\WSelector\b_weapon_circle");
            overlay_dock = Game.Content.Load<Texture2D>(@"UI\upper_dock_overlay");
            top_menu_border = Game.Content.Load<Texture2D>(@"UI\top_screen_border");
            asteroids_bar = Game.Content.Load<Texture2D>(@"UI\asteroids_bar");
            asteroids_x = Game.Content.Load<Texture2D>(@"UI\asteroid_x");
            audio_on = Game.Content.Load<Texture2D>(@"UI\audio_on");
            audio_off = Game.Content.Load<Texture2D>(@"UI\audio_off");
            audio_bar = Game.Content.Load<Texture2D>(@"UI\audio_bar");

            double_tap = Game.Content.Load<Texture2D>("Menu/double_tap");
            single_tap = Game.Content.Load<Texture2D>("Menu/single_tap");
            long_tap = Game.Content.Load<Texture2D>("Menu/long_tap");
            font_32 = Game.Content.Load<SpriteFont>("Fonts/OCRAExtended30");
            font_16 = Game.Content.Load<SpriteFont>("Fonts/OCRAExtended16");
            font12 = Game.Content.Load<SpriteFont>("Fonts/OCRAExtended12");


            tutorial1 = Game.Content.Load<Texture2D>(@"Tutorial\Tutorial1");
            tutorial2 = Game.Content.Load<Texture2D>(@"Tutorial\Tutorial2");
            tutorial3 = Game.Content.Load<Texture2D>(@"Tutorial\Tutorial3");
            tutorial4 = Game.Content.Load<Texture2D>(@"Tutorial\Tutorial4");
            tutorial5 = Game.Content.Load<Texture2D>(@"Tutorial\Tutorial5");
            tutorial6 = Game.Content.Load<Texture2D>(@"Tutorial\Tutorial6");
            tutorial7 = Game.Content.Load<Texture2D>(@"Tutorial\Tutorial7");
            tutorial8 = Game.Content.Load<Texture2D>(@"Tutorial\Tutorial8");
            tutorial9 = Game.Content.Load<Texture2D>(@"Tutorial\Tutorial9");

            tutorial_buttons = CreateRightAlignedButtons(new string[] { Strings.Prev, Strings.ExitTutorial, Strings.Next }, 250, 100, 400, 10, 0);

            top_left = new Rectangle(90, 60, 0, 0);
            middle_left = new Rectangle(90, 230, 0, 0);

            mini_top_left = new Rectangle(90, 60, 0, 0);
            mini_middle_left = new Rectangle(90, 170, 0, 0);
            mini_bottom_left = new Rectangle(90, 280, 0, 0);

            mini_top_middle = new Rectangle(400, 115, 0, 0);
            mini_bottom_middle = new Rectangle(400, 215, 0, 0);

            f_weapon_circle_pos = new Vector2(GraphicsDevice.Viewport.Width / 2.0f - f_weapon_circle.Width / 2.0f,
                GraphicsDevice.Viewport.Height / 2.0f - f_weapon_circle.Height / 2.0f);
            s_weapon_circle_pos = new Vector2(GraphicsDevice.Viewport.Width / 2.0f - s_weapon_circle.Width / 2.0f,
                GraphicsDevice.Viewport.Height / 2.0f - s_weapon_circle.Height / 2.0f);
            half_yglow_vec = new Vector2(yellow_glow.Width / 2.0f, yellow_glow.Height / 2.0f);

            score_dock_pos = new Vector2(GraphicsDevice.Viewport.Width - score_dock.Width - menu_dock.Width + 3, 0);

            menu_pos = new Vector2(GraphicsDevice.Viewport.Width - menu_dock.Width, 0);
            overlay_dock_pos = Vector2.Zero;

            tap_text_pos = new Vector2(GraphicsDevice.Viewport.Width / 2.0f - menu_f.MeasureString(tap_text).X / 2.0f,
                GraphicsDevice.Viewport.Height / 2.0f + s_weapon_circle.Height / 2.0f + 20.0f);

            pause_rect = new Rectangle(GraphicsDevice.Viewport.Width - menu_dock.Width,
                0, menu_dock.Width, menu_dock.Height);

            var resume_sm = menu_f.MeasureString(Strings.Resume);
            resume = new Button
            {
                CollisionBox = new Rectangle(590 + (100 - (int)resume_sm.X / 2) - 10, 261, (int)resume_sm.X + 20, (int)resume_sm.Y + 20),
                Label = Strings.Resume,
                Position = new Vector2(590 + (100 - resume_sm.X / 2.0f), 271)
            };


            var control_sm = menu_f.MeasureString(Strings.PauseMenu_Controls);
            controls = new Button
            {
                CollisionBox = new Rectangle(590 + (100 - (int)control_sm.X / 2), 311, (int)control_sm.X + 20, (int)control_sm.Y + 20),
                Label = Strings.PauseMenu_Controls,
                Position = new Vector2(590 + (100 - control_sm.X / 2.0f), 321)
            };
            var quit_sm = menu_f.MeasureString(Strings.Quit);
            quit_to_menu = new Button
            {
                CollisionBox = new Rectangle(590 + (100 - (int)quit_sm.X / 2), 411, (int)quit_sm.X + 20, (int)quit_sm.Y + 20),
                Label = Strings.Quit,
                Position = new Vector2(590 + (100 - quit_sm.X / 2.0f), 421)
            };
            var quit_save_sm = menu_f.MeasureString(Strings.QuitSave);
            quit_and_save = new Button
            {
                CollisionBox = new Rectangle(590 + (100 - (int)quit_save_sm.X / 2), 361, (int)quit_save_sm.X + 20, (int)quit_save_sm.Y + 20),
                Label = Strings.QuitSave,
                Position = new Vector2(590 + (100 - quit_save_sm.X / 2.0f), 371)
            };

            audio = new Button
            {
                CollisionBox = new Rectangle(0, 0, audio_on.Width, audio_on.Height),
                Label = Strings.AudioOn,
                Position = new Vector2(0, 0)
            };

            var back_sm = font_32.MeasureString(Strings.Back);
            back = new Button
            {
                CollisionBox = new Rectangle((int)(GraphicsDevice.Viewport.Width / 2.0f - back_sm.X / 2.0f) - 10, 410,
                    (int)back_sm.X + 20, (int)back_sm.Y + 20),
                Label = Strings.Back,
                Position = new Vector2(GraphicsDevice.Viewport.Width / 2.0f - back_sm.X / 2.0f, 420.0f)
            };

            box = new Box(new Texture2D[] { Game.Content.Load<Texture2D>("UI/pause_menu_border"),
            Game.Content.Load<Texture2D>("UI/WhitePixel"), Game.Content.Load<Texture2D>("UI/pause_menu_border") },
                initial_box_pos, box_dimension, Color.Black, 0.7f);

            weapons = new Button[11];

            weapons[0] = new Button { CollisionBox = new Rectangle(473 - 47, 141 - 43, 94, 86), Label = "", Position = new Vector2(473, 141), Pressed = false };
            weapons[1] = new Button { CollisionBox = new Rectangle(512 - 47, 282 - 43, 94, 86), Label = "", Position = new Vector2(512, 282), Pressed = false };
            weapons[2] = new Button { CollisionBox = new Rectangle(407 - 47, 359 - 43, 94, 86), Label = "", Position = new Vector2(407, 359), Pressed = false };
            weapons[3] = new Button { CollisionBox = new Rectangle(294 - 47, 275 - 43, 94, 86), Label = "", Position = new Vector2(294, 275), Pressed = false };
            weapons[4] = new Button { CollisionBox = new Rectangle(333 - 47, 142 - 43, 94, 86), Label = "", Position = new Vector2(333, 142), Pressed = false };
            weapons[5] = new Button { CollisionBox = new Rectangle(478 - 47, 137 - 43, 94, 86), Label = "", Position = new Vector2(478, 137), Pressed = false };
            weapons[6] = new Button { CollisionBox = new Rectangle(522 - 47, 261 - 43, 94, 86), Label = "", Position = new Vector2(522, 261), Pressed = false };
            weapons[7] = new Button { CollisionBox = new Rectangle(447 - 47, 349 - 43, 94, 86), Label = "", Position = new Vector2(447, 349), Pressed = false };
            weapons[8] = new Button { CollisionBox = new Rectangle(322 - 47, 326 - 43, 94, 86), Label = "", Position = new Vector2(322, 326), Pressed = false };
            weapons[9] = new Button { CollisionBox = new Rectangle(283 - 47, 224 - 43, 94, 86), Label = "", Position = new Vector2(283, 224), Pressed = false };
            weapons[10] = new Button { CollisionBox = new Rectangle(351 - 47, 134 - 43, 94, 86), Label = "", Position = new Vector2(351, 134), Pressed = false };

            //ChangeActiveWeapon((int)state_provider.CurrentState.player.AsShip.ship_description.PrimaryWeapons[PrimaryActive].Type);
            //ChangeActiveWeapon((int)state_provider.CurrentState.player.AsShip.ship_description.SecondaryWeapons[SecondaryActive].Type);

            //if (last_ship_selected == state_provider.CurrentState.player.AsShip.ship_type)
            //{
            //  state_provider.CurrentState.player.primary_active = PrimaryActive;
            //  state_provider.CurrentState.player.secondary_active = SecondaryActive;
            //}

            //last_ship_selected = state_provider.CurrentState.player.AsShip.ship_type;

            wtype = new Button[2];

            wtype[1] = new Button { CollisionBox = new Rectangle(437, 216, 61, 40), Label = "", Position = new Vector2(467, 236), Pressed = false };
            wtype[0] = new Button { CollisionBox = new Rectangle(312, 216, 61, 40), Label = "", Position = new Vector2(342, 236), Pressed = false };

            life_rect = new Rectangle(30, 0, life_bar.Width, life_bar.Height);
            grow_rect = new Rectangle(0, 3, life_bar.Width, life_bar.Height - 6);
            black_rect = new Rectangle(0, 0, 800, 25);
            top_menu_border_rect = new Rectangle(0, 25, top_menu_border.Width, top_menu_border.Height);
            asteroids_bar_rect = new Rectangle((int)score_dock_pos.X - asteroids_bar.Width + 3, 0, asteroids_bar.Width, asteroids_bar.Height);
            asteroids_x_rect = new Rectangle(0, 2, asteroids_x.Width, asteroids_x.Height);
            audio_rect = new Rectangle(0, 0, audio_on.Width, audio_on.Height);
            audio_rect_bar = new Rectangle(0, 0, audio_bar.Width, audio_bar.Height);

            anim1 = new xAnimations(sprite_batch, 1, state_provider, asteroids_bar_rect, asteroids_x, asteroids_x_rect);
            anim2 = new xAnimations(sprite_batch, 2, state_provider, asteroids_bar_rect, asteroids_x, asteroids_x_rect);
            anim3 = new xAnimations(sprite_batch, 3, state_provider, asteroids_bar_rect, asteroids_x, asteroids_x_rect);

        }
        public override void Update(GameTime gameTime)
        {
            box.Update((float)gameTime.ElapsedGameTime.TotalSeconds * 3);

            if (state == PlayState.Pause) state_provider.Enabled = false;

            anim1.Update(gameTime);
            anim2.Update(gameTime);
            anim3.Update(gameTime);

            base.Update(gameTime);
        }
        AudioManager audio_manager;
        void on_tap(Vector2 tap_position)
        {
            var t_p = tap_position.ToPoint(screen_transform);
            var play_click = false;


            switch (state)
            {

                case PlayState.Controls:
                    for (int i = 0; i < tutorial_buttons.Count(); i++)
                    {

                        var tmp = input_manager.IsBackButtonPressed();

                        if (tutorial_buttons[i].CollisionBox.Contains(t_p) || tmp)
                        {
                            if (i == 0 && (tutorial_page > 0))
                            {
                                play_click = true;
                                tutorial_page--;
                            }
                            else if (i == 1 || tmp)
                            {

                                state = PlayState.Pause;
                                play_click = true;
                                tutorial_page = 0;
                            }
                            else if (i == 2 && (tutorial_page < 2))
                            {
                                play_click = true;
                                tutorial_page++;
                            }
                        }
                    }
                    //if (back.CollisionBox.Contains(t_p) || input_manager.IsBackButtonPressed())
                    //{
                    //  state = PlayState.Pause;
                    //  play_click = true;
                    //}
                    break;
                case PlayState.Pause:
                    state_provider.Enabled = false;

                    

                    if (resume.CollisionBox.Contains(t_p) || pause_rect.Contains(t_p) || input_manager.IsBackButtonPressed())
                    {
                        state_provider.Enabled = true;
                        state = PlayState.Playing;
                        box.StartTransition(initial_box_pos, box_dimension);
                        audio_manager.playAllSounds();
                        play_click = true;
                    }
                    else if (controls.CollisionBox.Contains(t_p))
                    {
                        play_click = true;
                        state = PlayState.Controls;
                    }
                    else if (quit_to_menu.CollisionBox.Contains(t_p))
                    {
                        play_click = true;
                        if (OnQuitToMenu != null)
                        {
                            audio_manager.stopAllSounds();
                            OnQuitToMenu();
                        }
                    }
                    else if (quit_and_save.CollisionBox.Contains(t_p))
                    {
                        var tmp = Leaderboard.GetWakaModeStats();
                        play_click = true;
                        Leaderboard.SaveWakaModeStats(new WakaShipModeEntry()
                        {
                            TimeToEndGame = state_provider.CurrentScore.TimeToEndGame,
                            BossDestroyed = state_provider.CurrentScore.BossDestroyed,
                            ShipsDestroyed = state_provider.CurrentScore.ShipsDestroyed,
                            LevelScore = state_provider.CurrentScore.LevelScore,
                            Level = state_provider.CurrentState.level,
                            AudioState = AudioManager.Volume > 0,
                            AsteroidHits = state_provider.CurrentState.num_of_asteroids_hit,
                            TimeToAddOrRemove = state_provider.CurrentState.time_to_add_or_remove,
                            showAlwaysTutorial = tmp.showAlwaysTutorial
                        }
                        );
                        StateManager.SelectMenu(Game);
                        audio_manager.stopAllSounds();
                    }
                    break;
                case PlayState.Playing:


                    if (pause_rect.Contains(t_p) || input_manager.IsBackButtonPressed())
                    {
                        state_provider.Enabled = false;
                        state = PlayState.Pause;
                        audio_manager.pauseAllSounds();
                        box.StartTransition(final_box_pos, box_dimension);
                        play_click = true;

                    }
                    else
                        if (audio.CollisionBox.Contains(t_p))
                        {

                            if (audio_manager.AudioState)
                            {
                                audio_manager.OnaudioOff();
                            }
                            else
                            {
                                audio_manager.OnaudioOn();
                            }
                        }
                    break;
            }
            if (audio_manager.AudioState && play_click)
                click.Play(AudioManager.Volume, 0.0f, 0.0f);
        }

        private void CloseWeaponSelector()
        {
            state = PlayState.Playing;
            for (int i = 0; i < wtype.Length; i++) wtype[i].Pressed = false;
            click.Play(AudioManager.Volume, 0.0f, 0.0f);

            state_provider.Enabled = true;
        }

        public event Action OnQuitToMenu;

        private void ChangeActiveWeapon(int index)
        {
            int init_v = 0;
            int fin_v = index < 5 ? 5 : weapons.Length;

            for (int i = init_v; i < fin_v; i++) weapons[i].Pressed = false;

            weapons[index].Pressed = true;
        }

        static StringBuilder score_stringB = new StringBuilder("0");

        Color final_bar_color = new Color(18, 255, 0), life_bar_color = new Color(135, 0, 3);
        Color life_color = Color.Green;
        Color grow_color = Color.White;

        public override void Draw(GameTime gameTime)
        {
            sprite_batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, screen_transform);

            switch (state)
            {
                case PlayState.Playing:
                    DrawGame();
                    break;
                case PlayState.Pause:
                    DrawGame();
                    if (!box.InTransition) alpha = MathHelper.Clamp(alpha + (float)gameTime.ElapsedGameTime.TotalSeconds * 8.0f, 0.0f, 1.0f);
                    DrawPauseMenu();
                    break;
                case PlayState.Controls:
                    DrawControls();
                    break;
            }

            sprite_batch.End();

            base.Draw(gameTime);
        }


        StringBuilder s1 = new StringBuilder(), s2 = new StringBuilder();

        #region Draw Helper

        Vector2 score_dock_pos, score_pos = new Vector2(0, 5), menu_pos, ui_dock_pos = new Vector2(0, 0);
        Rectangle life_rect, grow_rect, asteroids_bar_rect, asteroids_x_rect, audio_rect, audio_rect_bar;
        private void DrawGame()
        {
            sprite_batch.Draw(black_box, black_rect, Color.Black);



            if (state_provider.CurrentState.time_to_add_or_remove > 0.0)
            {
                var tmp = (int)(life_bar.Width * ((state_provider.CurrentScore.TimeToEndGame) / BaseLevelStats.MaxTime));
                grow_rect.X = tmp - 30;
                grow_rect.Width = (int)(life_bar.Width * ((state_provider.CurrentScore.TimeToEndGame + state_provider.CurrentState.time_to_add_or_remove) / BaseLevelStats.MaxTime)) - tmp + 50;
                sprite_batch.Draw(life_bar, grow_rect, grow_color);
            }

            life_rect.Width = (int)(life_bar.Width * (state_provider.CurrentScore.TimeToEndGame / BaseLevelStats.MaxTime));
            sprite_batch.Draw(life_bar, life_rect, life_color);
            sprite_batch.Draw(top_menu_border, top_menu_border_rect, Color.White);


            sprite_batch.Draw(score_dock, score_dock_pos, Color.White);
            sprite_batch.Draw(menu_dock, menu_pos, Color.White);

            var score_val = state_provider.CurrentScore.LevelScore.ToString();
            score_pos.X = score_dock_pos.X - menu_f.MeasureString(score_val).X / 2.0f + score_dock.Width / 2;
            sprite_batch.DrawString(menu_f, score_val, score_pos, Color.White);
            sprite_batch.Draw(asteroids_bar, asteroids_bar_rect, Color.White);




            anim1.Draw();
            anim2.Draw();
            anim3.Draw();

            sprite_batch.Draw(audio_bar, audio_rect_bar, Color.White);
            var audio_manager = Game.Services.GetService(typeof(IAudioManager)) as AudioManager;

            if (audio_manager.AudioState)
                sprite_batch.Draw(audio_on, audio_rect, Color.White);
            else
                sprite_batch.Draw(audio_off, audio_rect, Color.White);

            box.Draw(sprite_batch);
        }

        Texture2D f_weapon_circle, s_weapon_circle, yellow_glow, white_glow;
        string tap_text = "Tap to Close";
        Vector2 tap_text_pos, f_weapon_circle_pos, s_weapon_circle_pos, half_yglow_vec;
        private Texture2D menu_dock, black_box, asteroids_bar, asteroids_x, audio_off, audio_on, audio_bar;
        private Vector2 overlay_dock_pos;
        private void DrawWeaponSelector()
        {
            //sprite_batch.Draw(f_weapon_circle, f_weapon_circle_pos, Color.White);
            //sprite_batch.DrawString(menu_f, tap_text, tap_text_pos, Color.White);
            //if (wtype[0].Pressed || wtype[1].Pressed)
            //{
            //  sprite_batch.Draw(s_weapon_circle, s_weapon_circle_pos, Color.White);

            //  if (wtype[0].Pressed)
            //  {
            //    sprite_batch.Draw(yellow_glow, wtype[0].Position - half_yglow_vec, Color.White);

            //    for (int i = 0; i < state_provider.CurrentState.player.AsShip.ship_description.PrimaryWeapons.Count; i++)
            //    {
            //      var index = (int)state_provider.CurrentState.player.AsShip.ship_description.PrimaryWeapons[i].Type;
            //      var t = Game.Content.Load<Texture2D>("UI/WSelector/" + ships.Weapons[index].Type.ToString());
            //      if (weapons[index].Pressed)
            //        sprite_batch.Draw(white_glow, weapons[index].Position - half_yglow_vec, Color.White);
            //      sprite_batch.Draw(t, weapons[index].Position - new Vector2(t.Width / 2.0f, t.Height / 2.0f), Color.White);
            //    }
            //  }
            //  else
            //  {
            //    sprite_batch.Draw(yellow_glow, wtype[1].Position - half_yglow_vec, Color.White);

            //    for (int i = 0; i < state_provider.CurrentState.player.AsShip.ship_description.SecondaryWeapons.Count; i++)
            //    {
            //      var index = (int)state_provider.CurrentState.player.AsShip.ship_description.SecondaryWeapons[i].Type;
            //      var t = Game.Content.Load<Texture2D>("UI/WSelector/" + ships.Weapons[index].Type.ToString());
            //      if (weapons[index].Pressed)
            //        sprite_batch.Draw(white_glow, weapons[index].Position - half_yglow_vec, Color.White);
            //      sprite_batch.Draw(t, weapons[index].Position - new Vector2(t.Width / 2.0f, t.Height / 2.0f), Color.White);
            //    }
            //  }
            //}
        }

        private void DrawPauseMenu()
        {
            //var fm = title_font.MeasureString(Strings.GamePaused);
            //sprite_batch.Draw(pause_bg, new Vector2(CenterValue(480, pause_bg.Width), CenterValue(800, pause_bg.Height)), Color.White * alpha);
            //sprite_batch.DrawString(title_font, Strings.GamePaused, new Vector2(CenterValue(480, fm.X), 235) + Vector2.One * 2.0f, Color.SteelBlue * alpha);
            //sprite_batch.DrawString(title_font, Strings.GamePaused, new Vector2(CenterValue(480, fm.X), 235), Color.White * alpha);
            if (!box.InTransition)
            {
                sprite_batch.DrawString(menu_f, resume.Label, resume.Position, Color.White * alpha);
                sprite_batch.DrawString(menu_f, controls.Label, controls.Position, Color.White * alpha);
                sprite_batch.DrawString(menu_f, quit_to_menu.Label, quit_to_menu.Position, Color.White * alpha);
                sprite_batch.DrawString(menu_f, quit_and_save.Label, quit_and_save.Position, Color.White * alpha);
            }
        }

        private Button[] CreateRightAlignedButtons(string[] labels, int offset, int starting_x, int starting_y, int padding, int align_val)
        {
            Button[] bs = new Button[labels.Count()];

            for (int i = 0; i < bs.Count(); i++)
            {
                Vector2 label_measure = font_32.MeasureString(labels[i]);
                var x = starting_x + i * offset;
                //int x = (int)((w + align_val) / 2.0f - label_measure.X / 2.0f);
                int y = starting_y;
                bs[i] = new Button()
                {
                    CollisionBox = new Rectangle(x, y, (int)label_measure.X, (int)label_measure.Y),
                    Label = labels[i],
                    Position = new Vector2(x, y)
                };
            }

            return bs;
        }

        private void DrawControls()
        {

            sprite_batch.Draw(white_pixel, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height),
              Color.Black * MathHelper.Min(alpha, 0.8f));


            //sprite_batch.DrawString(font_16, Strings.Controls_SingleTap + "\n" + Strings.Controls_ST_Desc_OF.Replace("\\n", "\n") +
            //    "\n" + Strings.Controls_ST_Desc_OE.Replace("\\n", "\n") + "\n" +
            //    Strings.Controls_ST_Desc_OS.Replace("\\n", "\n"), new Vector2(61, 91), Color.DarkGray * alpha); // 150
            //sprite_batch.DrawString(font_16, Strings.Controls_SingleTap + "\n" + Strings.Controls_ST_Desc_OF.Replace("\\n", "\n") +
            //    "\n" + Strings.Controls_ST_Desc_OE.Replace("\\n", "\n") + "\n" +
            //    Strings.Controls_ST_Desc_OS.Replace("\\n", "\n"), new Vector2(60, 90), Color.White * alpha); // 150

            //sprite_batch.Draw(double_tap, new Vector2(85, 360), Color.White * alpha);


            //sprite_batch.DrawString(font_16, Strings.Pinch.Replace("\\n", "\n"), new Vector2(421, 211), Color.DarkGray * alpha);
            //sprite_batch.DrawString(font_16, Strings.Pinch.Replace("\\n", "\n"), new Vector2(420, 210), Color.White * alpha);

            switch (tutorial_page)
            {
                case 0:

                    sprite_batch.DrawString(font_16,Strings.TitleTutorial1 +" \n"+ Strings.Tutorial1.Replace("\\n", "\n"),
                        new Vector2(421, 60), Color.DarkGray * alpha);
                    sprite_batch.DrawString(font_16, Strings.TitleTutorial1 + " \n" + Strings.Tutorial1.Replace("\\n", "\n"),
                        new Vector2(420, 60), Color.White * alpha);

                    sprite_batch.DrawString(font_16, Strings.Tutorial2.Replace("\\n", "\n"),
                        new Vector2(421, 230), Color.DarkGray * alpha);
                    sprite_batch.DrawString(font_16, Strings.Tutorial2.Replace("\\n", "\n"),
                        new Vector2(420, 230), Color.White * alpha);

                    top_left.Width = tutorial2.Width;
                    top_left.Height = tutorial2.Height;
                    sprite_batch.Draw(tutorial2, top_left, Color.White * alpha);

                    middle_left.Width = tutorial1.Width;
                    middle_left.Height = tutorial1.Height;
                    sprite_batch.Draw(tutorial1, middle_left, Color.White * alpha);

                    for (int i = 1; i < tutorial_buttons.Count(); i++)
                    {
                        sprite_batch.DrawString(font_32, tutorial_buttons[i].Label, tutorial_buttons[i].Position + Vector2.One * 2.5f, Color.Black * alpha);
                        sprite_batch.DrawString(font_32, tutorial_buttons[i].Label, tutorial_buttons[i].Position, Color.White * alpha);
                    }

                    break;
                case 2:

                    sprite_batch.DrawString(font_16, Strings.Tutorial3.Replace("\\n", "\n"),
                        new Vector2(mini_top_left.X + tutorial3.Width + 3, mini_top_left.Y), Color.DarkGray * alpha);
                    sprite_batch.DrawString(font_16, Strings.Tutorial3.Replace("\\n", "\n"),
                        new Vector2(mini_top_left.X + tutorial3.Width + 4, mini_top_left.Y), Color.White * alpha);

                    sprite_batch.DrawString(font_16, Strings.Tutorial4.Replace("\\n", "\n"),
                        new Vector2(mini_middle_left.X + tutorial4.Width + 3, mini_middle_left.Y), Color.DarkGray * alpha);
                    sprite_batch.DrawString(font_16, Strings.Tutorial4.Replace("\\n", "\n"),
                        new Vector2(mini_middle_left.X + tutorial4.Width + 4, mini_middle_left.Y), Color.White * alpha);

                    sprite_batch.DrawString(font_16, Strings.Tutorial5.Replace("\\n", "\n"),
                        new Vector2(mini_bottom_left.X + tutorial5.Width + 3, mini_bottom_left.Y - 10), Color.DarkGray * alpha);
                    sprite_batch.DrawString(font_16, Strings.Tutorial5.Replace("\\n", "\n"),
                        new Vector2(mini_bottom_left.X + tutorial5.Width + 4, mini_bottom_left.Y - 10), Color.White * alpha);

                    sprite_batch.DrawString(font_16, Strings.Tutorial6.Replace("\\n", "\n"),
                        new Vector2(mini_top_middle.X + tutorial6.Width + 3, mini_top_middle.Y ), Color.DarkGray * alpha);
                    sprite_batch.DrawString(font_16, Strings.Tutorial6.Replace("\\n", "\n"),
                        new Vector2(mini_top_middle.X + tutorial6.Width + 4, mini_top_middle.Y ), Color.White * alpha);

                    sprite_batch.DrawString(font_16, Strings.Tutorial7.Replace("\\n", "\n"),
                        new Vector2(mini_bottom_middle.X + tutorial7.Width + 3, mini_bottom_middle.Y -10), Color.DarkGray * alpha);
                    sprite_batch.DrawString(font_16, Strings.Tutorial7.Replace("\\n", "\n"),
                        new Vector2(mini_bottom_middle.X + tutorial7.Width + 4, mini_bottom_middle.Y -10), Color.White * alpha);

                    mini_top_left.Width = tutorial3.Width;
                    mini_top_left.Height = tutorial3.Height;
                    sprite_batch.Draw(tutorial3, mini_top_left, Color.White * alpha);

                    mini_middle_left.Width = tutorial4.Width;
                    mini_middle_left.Height = tutorial4.Height;
                    sprite_batch.Draw(tutorial4, mini_middle_left, Color.White * alpha);

                    mini_bottom_left.Width = tutorial5.Width;
                    mini_bottom_left.Height = tutorial5.Height;
                    sprite_batch.Draw(tutorial5, mini_bottom_left, Color.White * alpha);

                    mini_top_middle.Width = tutorial6.Width;
                    mini_top_middle.Height = tutorial6.Height;
                    sprite_batch.Draw(tutorial6, mini_top_middle, Color.White * alpha);

                    mini_bottom_middle.Width = tutorial7.Width;
                    mini_bottom_middle.Height = tutorial7.Height;
                    sprite_batch.Draw(tutorial7, mini_bottom_middle, Color.White * alpha);
                    for (int i = 0; i < tutorial_buttons.Count() - 1; i++)
                    {


                        sprite_batch.DrawString(font_32, tutorial_buttons[i].Label, tutorial_buttons[i].Position + Vector2.One * 2.5f, Color.Black * alpha);
                        sprite_batch.DrawString(font_32, tutorial_buttons[i].Label, tutorial_buttons[i].Position, Color.White * alpha);
                    }

                    break;
                case 1:

                    sprite_batch.DrawString(font_16,Strings.TitleTutorial8 +" \n"+ Strings.Tutorial8.Replace("\\n", "\n"),
                        new Vector2(421, 60), Color.DarkGray * alpha);
                    sprite_batch.DrawString(font_16, Strings.TitleTutorial8 + " \n" + Strings.Tutorial8.Replace("\\n", "\n"),
                        new Vector2(420, 60), Color.White * alpha);

                    sprite_batch.DrawString(font_16, Strings.Tutorial9.Replace("\\n", "\n"),
                        new Vector2(421, 230), Color.DarkGray * alpha);
                    sprite_batch.DrawString(font_16, Strings.Tutorial9.Replace("\\n", "\n"),
                        new Vector2(420, 230), Color.White * alpha);

                    top_left.Width = tutorial8.Width;
                    top_left.Height = tutorial8.Height;
                    sprite_batch.Draw(tutorial8, top_left, Color.White * alpha);

                    middle_left.Width = tutorial9.Width;
                    middle_left.Height = tutorial9.Height;
                    sprite_batch.Draw(tutorial9, middle_left, Color.White * alpha);

                    for (int i = 0; i < tutorial_buttons.Count(); i++)
                    {
                        sprite_batch.DrawString(font_32, tutorial_buttons[i].Label, tutorial_buttons[i].Position + Vector2.One * 2.5f, Color.Black * alpha);
                        sprite_batch.DrawString(font_32, tutorial_buttons[i].Label, tutorial_buttons[i].Position, Color.White * alpha);
                    }

                    break;

            }
        }

        #endregion

        private float CenterValue(float screen_dim, float item_width)
        {
            return screen_dim / 2.0f - item_width / 2.0f;
        }
    }

}
public class xAnimations
{
    int cross_number;
    IStateProvider<BaseLevelStats> state_provider;
    Rectangle asteroids_bar_rect, asteroids_x_rect;
    private Vector2 startPosition = new Vector2(300, 100);
    Vector2 toPosition;
    bool isActive = false;
    Texture2D asteroids_x;
    Vector2 current_position;
    SpriteBatch sprite_batch;
    float remainingTime;
    float pass;

    public xAnimations(SpriteBatch spriteBatch, int cross, IStateProvider<BaseLevelStats> stat, Rectangle AstBarRect, Texture2D asteroindT, Rectangle asteroids_x_rectangle)
    {
        remainingTime = State.X_ANIMATION_DURATION;
        state_provider = stat;
        cross_number = cross;
        asteroids_bar_rect = AstBarRect;
        toPosition = new Vector2(asteroids_bar_rect.X + 15 + (cross_number - 1) * 30, 2);
        asteroids_x = asteroindT;
        asteroids_x_rect = asteroids_x_rectangle;
        sprite_batch = spriteBatch;
    }

    public void Update(GameTime gameTime)
    {

        if (state_provider.CurrentState.num_of_asteroids_hit < cross_number)
        {
            isActive = false;
            remainingTime = State.X_ANIMATION_DURATION;
        }

        if (remainingTime == State.X_ANIMATION_DURATION && state_provider.CurrentState.num_of_asteroids_hit >= cross_number)
        {
            isActive = true;
        }


        pass = MathHelper.SmoothStep(1.0f, 0.01f, remainingTime / State.X_ANIMATION_DURATION);


        if (pass > 0 && pass <= 1 && isActive)
        {
            current_position = Vector2.SmoothStep(startPosition, toPosition, pass);

            var tmp1 = (float)(-10.0 * pass + 10.0) + 1;
            var rect_width = (int)(asteroids_x.Width * tmp1);
            var rect_height = (int)(asteroids_x.Height * tmp1);

            asteroids_x_rect.X = (int)current_position.X;
            asteroids_x_rect.Y = (int)current_position.Y;

            asteroids_x_rect.Width = rect_width;
            asteroids_x_rect.Height = rect_height;
            remainingTime = Math.Max(0.0f, remainingTime - (float)gameTime.ElapsedGameTime.TotalSeconds);
        }
        else if (isActive)
        {
            asteroids_x_rect.X = (int)toPosition.X;
            asteroids_x_rect.Y = (int)toPosition.Y;
            asteroids_x_rect.Width = asteroids_x.Width;
            asteroids_x_rect.Height = asteroids_x.Height;
        }
    }

    public void Draw()
    {
        if (isActive)
        {
            sprite_batch.Draw(asteroids_x, asteroids_x_rect, new Color(255.0f, 255.0f, 255.0f, pass));
        }
    }


}
